/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.config.javabeans;

import java.io.Serializable;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/**
 * This class corresponds to the "references" element of the reference_conf.xml.
 * It stores all the configured reference lists.
 */
public class JBReferenceMap implements Serializable
{
    /** table of base reference lists, key=lower case name, value= JBReferenceListDescriptor. */
    private Map baserefMap = new HashMap();

    /** table of search reference lists, key=lower case name, value= JBReferenceListDescriptor. */
    private Map searchrefMap = new HashMap();

    /**
     * Adds a base reference list.
     *
     * @param desc base reference list
     */
    public void addBaseReferenceDesc(JBBaseReferenceListDescriptor desc)
    {
        baserefMap.put(desc.getName().trim().toLowerCase(), desc);
    }

    /**
     * Adds a search reference list.
     *
     * @param desc search reference list
     */
    public void addSearchReferenceDesc(JBSearchReferenceListDescriptor desc)
    {
        searchrefMap.put(desc.getName().trim().toLowerCase(), desc);
    }

    /**
     * Gets base reference lists descriptors iterator.
     *
     * @return iterator
     */
    public Iterator basevaluesIterator()
    {
        return baserefMap.values().iterator();
    }

    /**
     * Gets search reference lists descriptors iterator.
     *
     * @return iterator
     */
    public Iterator searchvaluesIterator()
    {
        return searchrefMap.values().iterator();
    }

    /**
     * Gets a base reference list descriptor from its name.
     *
     * @param name base reference list name
     *
     * @return base reference list descriptor
     */
    public JBBaseReferenceListDescriptor findBaseRefDescByName(String name)
    {
        // rendre case insensitive en mettant le param en lowercase
        String key = name.trim().toLowerCase();

        return (JBBaseReferenceListDescriptor) baserefMap.get(key);
    }

    /**
     * Gets a search reference list descriptor from its name.
     *
     * @param name search reference list name
     *
     * @return search reference list descriptor
     */
    public JBSearchReferenceListDescriptor findSearchRefDescByName(String name)
    {
        // rendre case insensitive en mettant le param en lowercase
        String key = name.trim().toLowerCase();

        return (JBSearchReferenceListDescriptor) searchrefMap.get(key);
    }

    /**
     * Displays the list of configured reference lists.
     *
     * @return String formatted list
     */
    @Override
	public String toString()
    {
        StringBuffer buf = new StringBuffer();
        java.util.Iterator it = null;

        if (baserefMap != null)
        {
            buf.append("Map components of baserefMap = ");
            buf.append(System.getProperty("line.separator"));
            it = baserefMap.values().iterator();

            Object key;

            while (it.hasNext())
            {
                key = it.next();
                buf.append(key);
                buf.append(System.getProperty("line.separator"));
            }
        }

        if (searchrefMap != null)
        {
            buf.append("Map components of searchrefMap = ");
            buf.append(System.getProperty("line.separator"));
            it = searchrefMap.values().iterator();

            Object key;

            while (it.hasNext())
            {
                key = it.next();
                buf.append(key);
                buf.append(System.getProperty("line.separator"));
            }
        }

        return buf.toString();
    }

    // end of toString method
}
